// SPDX-FileCopyrightText: 2023-2025 Sangfor Technologies Inc.
// SPDX-License-Identifier: Mulan PSL v2
package com.geniusai.aip.auth.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.geniusai.aip.auth.domain.Notebooks;
import com.geniusai.aip.auth.domain.TotalResource;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * @Entity generate.Notebooks
 */
@Mapper
public interface NotebooksMapper extends BaseMapper<Notebooks> {

    @Select("select id,\n" +
            "       name,\n" +
            "       user_id,\n" +
            "       description,\n" +
            "       image,\n" +
            "       cpu_num,\n" +
            "       vgpu_display_name,\n" +
            "       ifnull(vgpu_memory, 0) vgpu_memory,\n" +
            "       ifnull(memory_size, 0) memory_size,\n" +
            "       volume_name,\n" +
            "       workspace_size,\n" +
            "       update_time,\n" +
            "       create_time,\n" +
            "       last_active,\n" +
            "       project_id,\n" +
            "       status\n" +
            "from aip.notebooks" +
            " where user_id = #{userId}")
    List<Notebooks> selectNotebooks(String userId);

    @Select("select sum(vgpu_num) value, vgpu_display_name as name from aip.notebooks group by vgpu_display_name")
    @MapKey("name")
    Map<String, TotalResource.VGpuGraph> selectVGpuMemoryGroupByVGpuDisplayName();

    @Select("select ifnull(sum(vgpu_memory), 0) from aip.notebooks")
    Integer sumVGpuMemory();
}